package jam.UserInterface.UI;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import javax.imageio.ImageIO;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.text.AttributeSet;
import javax.swing.text.SimpleAttributeSet;

	public class colourChooserWindow extends JDialog implements ActionListener{


		private JFrame colourWindow = new JFrame();
		private JFrame mainFrame;
		private String username;
		private JColorChooser colorChooser;
		private SimpleAttributeSet attributes;
		private Color newColor;
		private JPanel controlPanel = new JPanel();
		private JButton okButton = new JButton();
		private JButton cancelButton = new JButton();
		private JEditorPane chatLog;
		private Image avatarPic;
    
// some of this code was taken from the book Java Swing 2nd edition by Marc Loy, Robert Eckstein, Dave Wood, James Elliott and Brian Cole, published in 2002 by O'Reilly, USA.

		public colourChooserWindow(JFrame mainFrame, String username, JEditorPane chatLog, Image pic){
			this.username = username;
			this.mainFrame = mainFrame;
			this.chatLog = chatLog;
			this.avatarPic = pic;
			Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
			colourWindow.setBounds(((int)(screenSize.getWidth()-400))/2,((int)(screenSize.getHeight()-400))/2,400,400);
			colourWindow.setLayout(new BorderLayout());


			colorChooser = new JColorChooser(Color.black);
			colourWindow.add(colorChooser, BorderLayout.CENTER);

			okButton.setText("OK");
			okButton.addActionListener(this);

			cancelButton.setText("Cancel");
			cancelButton.addActionListener(this);

			controlPanel.add(okButton);
			controlPanel.add(cancelButton);

			colourWindow.add(controlPanel,BorderLayout.SOUTH);
			
			//change the icon code
			String currentDirectory = System.getProperty("user.dir");
			try {
				Image image = ImageIO.read(new File(currentDirectory + "/src/jam/UserInterface/Icons/JAMicon.png"));
				colourWindow.setIconImage(image);
			}catch (Exception e){System.out.println("The file could not be read!!!!");}
			
			colourWindow.setVisible(true);

		}

		public Color getNewColor(){return newColor;}

		public AttributeSet getAttributes(){return attributes;}

		@Override
		public void actionPerformed(ActionEvent e){
			if (e.getSource() == okButton){
				newColor = colorChooser.getColor();
				mainFrame.dispose();
				
				SwingUtilities.invokeLater(new Runnable(){
					public void run(){
						new mainWindow(newColor, username, chatLog, avatarPic); // creates the user interface
					}
				});

				colourWindow.dispose();
			}
			else if (e.getSource() == cancelButton){
				newColor = null;
				colourWindow.dispose();
			}
		}
}

